//
//  NewFeatureController.swift
//  mySina
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit
import SnapKit
//引导页照片的数量
private let imageCont:NSInteger = 4
private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {
    init() {
        //指定流水布局
        let layout = UICollectionViewFlowLayout()
        //修改布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //修改视图滚动方法
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置item上下左右间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        //开启分页效果
        collectionView?.pagingEnabled = true
        //关闭滚动条
        collectionView?.showsHorizontalScrollIndicator = false
        //取消弹簧效果
        collectionView?.bounces = false
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //绑定cell
        self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCont
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    //cell完成展示之后调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        print(indexPath.item)
        //获取准确的cell
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        //获取cell的indexPath
        let index = collectionView.indexPathForCell(cell)
//        print(index?.item)
        if index?.item == imageCont - 1 {
            //执行动画
            cell.showAnimation()
        }
    }
}
//在一个文件中，可以同时创建多个类
class NewFeatureCell:UICollectionViewCell {
    //添加index属性
    var index:Int = 0 {
        didSet {
            //修改Cell对应显示的image
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            //隐藏开始按钮
            startBtn.hidden = true
        }
    }
    //开始体验按钮的动画
    private func showAnimation() {
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.hidden = false
        /**
        衰变的动画效果
        iOS7.0推出的
        - parameter duration:               动画持续时间
        - parameter delay:                  动画延迟时间
        - parameter usingSpringWithDamping: 弹簧系数 区间 0.1 ~ 1  越小越弹
        - parameter initialSpringVelocity:  加速度
        - parameter options:                动画的可选项  swift中使用数组
        - parameter animations:             执行动画闭包
        - parameter completion:             动画执行完成的闭包
        */
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 2, options: [], animations: { () -> Void in
                self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        //添加约束
        //图片的自动布局
        //edges内边距
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        //按钮的自动布局
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        //开始按钮的点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
    }
    //开始按钮的监听
    @objc private func startBtnDidClick() {
//        print(__FUNCTION__)
        //动画结束的时候，发出界面切换的通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }
    //懒加载新特性界面
    lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //懒加载开始按钮
    lazy var startBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        //设置大小
        btn.sizeToFit()
        return btn
    }()
}
